import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-07-27
 * Time: 22:48
 */
public class SeqList {
    private int[] array;
    private int size;

    //构造方法
    public SeqList() {

    }
    public SeqList(int initcapacity) {
        this.array = new int[initcapacity];
    }

    //打印顺序表
    public void display() {
        for (int i = 0; i < this.size; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    //获取顺序表长度
    public int getSize() {
        return this.size;
    }

    //判断顺序表是否包含某个元素，若包含则返还true，否则返回false。
    public boolean contains(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素在顺序表的位置，如果顺序表包含这个元素，就返回它在顺序表中对应的位置，否则返回-1；
    public int indexOf(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //新增元素1
    public void add(int data) {
        if (isFull()) {
            scalingArray();
        }
        this.array[size] = data;
        this.size++;
    }
    //判断数组是否满了
    private boolean isFull() {
        return this.size == array.length;
    }
    //对数组进行扩容
    private void scalingArray() {
        this.array = Arrays.copyOf(this.array,2*this.array.length);//扩容为原来的两倍
    }

    //新增元素2
    public void add(int pos,int data) {
        try {
            isIllegal(pos);
            if (isFull()) {
                scalingArray();
            }
            for (int i = this.size - 1; i >= pos; i--) {
                array[i + 1] = array[i];
            }
            this.array[pos] = data;
            this.size++;
        }catch (PosIllegal e) {
            e.printStackTrace();
        }
    }
    //判断pos是否合法
    private void isIllegal(int pos) {
        if(pos < 0 || pos > this.size) {
            throw new PosIllegal("pos不合法！");
        }
    }

    //获取某个位置元素
    public int get(int pos) {
        try {
            isEmpty();
            isIllegal_1(pos);
            return this.array[pos];
        }catch (PosIllegal e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return -1;
    }
    //判断数组是否为空
    private void isEmpty() {
        if(this.size == 0) {
            throw new EmptyException("数组为空！");
        }
    }
    //判断pos是否合法2
    private void isIllegal_1(int pos) {
        if (pos < 0 || pos >= this.size) {
            throw new PosIllegal("pos不合法! ");
        }
    }

    //更改某个位置的元素
    public void set(int pos,int value) {
        try {
            isEmpty();
            isIllegal_1(pos);
            this.array[pos] = value;
        }catch (PosIllegal e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
    }

    //删除第一次出现的元素
    public void remove(int toRemove) {
        int x = indexOf(toRemove);
        if (x == -1) {   //x 等于-1说明找不到
            System.out.println("数组中没有该元素！");
            return;
        }
        for (int i = x; i < this.size - 1; i++) {
            this.array[i] = this.array[i + 1];
        }
        this.size--;
    }

    //清空顺序表
    public void clear() {
        this.size = 0;
    }


    public static void main(String[] args) {
        SeqList seqList = new SeqList(10);
        seqList.add(1);
        seqList.add(2);
        seqList.add(3);
        seqList.add(4);
        seqList.add(5);
        seqList.display();
        seqList.set(1,99);
        seqList.display();
    }
}
